package com.chenfan.filestore.utils;

import java.io.File;
import java.util.Date;
import java.util.UUID;

public class FileUtil {
    public static String suffix(String fileName){
        if(null == fileName || "".equals(fileName)) {
            return "";
        }
        if(!fileName.contains(".")) {
            return "";
        }
        return fileName.substring(fileName.lastIndexOf(".")+1);
    }

    public static String getUploadPath(String fileName){
        String yyyyMMdd = DateUtils.format(new Date(), "yyyyMMdd");
        return new StringBuffer().append(yyyyMMdd.substring(0, 4))
                .append("/").append(yyyyMMdd.substring(4, 6))
                .append("/").append(yyyyMMdd.substring(6, 8))
                .append("/").append(uuid()).append(".").append(suffix(fileName)).toString();
    }

    public static String getUploadDir(){
        String yyyyMMdd = DateUtils.format(new Date(), "yyyyMMdd");
        return new StringBuffer().append(yyyyMMdd.substring(0, 4))
                .append("/").append(yyyyMMdd.substring(4, 6))
                .append("/").append(yyyyMMdd.substring(6, 8))
                .append("/").toString();
    }

    public static Boolean createParentDir(String filePath){
        try {
            File dir = new File(filePath);
            if(!dir.exists()){
                dir.mkdirs();//创建目录
            }
//            File f = new File(filePath,fileName);
//            if(!f.exists()){
//                f.createNewFile();
//                return true;
//            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    /**
     * 获取无下划线uuid
     *
     * @return
     */

    public static String uuid() {
        return UUID.randomUUID().toString().replaceAll("\\-", "").toLowerCase();
    }

//    public static void main(String[] args) {
////        System.out.println(suffix("aaa.png"));
////        System.out.println(getName("aaa.jpg"));
//    }
}
